package com.fireline.beauty.member;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.widgets.ControlUtil;

/**
 * 选择套餐
 * */
public class XztcDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	/**
	 * @wbp.nonvisual location=37,482
	 */
	private final Dataset dataset = new Dataset(
			"select * from tcxx where\r\n? is null \r\nor tcbh like '%'|| ? ||'%'\r\nor tcmc like '%'|| ? ||'%'\r\nor jm   like '%'|| ? ||'%'\r\n",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=117,482
	 */
	private final Dataset tcfwxmSet = new Dataset(true,"tcfwxm",SqliteJdbcImpl.INSTANCE);

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public XztcDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		ControlUtil.centerShell(shell);
		Display display = getParent().getDisplay();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		dataset.setKeyColumns(new String[] {});
		tcfwxmSet.setKeyColumns(new String[] {});
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(465, 300);
		shell.setText("套餐选择");
		shell.setLayout(new GridLayout(1, false));

		DataTable dataTable = new DataTable(shell, SWT.BORDER
				| SWT.FULL_SELECTION, dataset);
		dataTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
		dataTable.setHeaderVisible(true);
		dataTable.setLinesVisible(true);

		DataTableColumn dataTableColumn = new DataTableColumn(dataTable,
				SWT.NONE, "tcbh");
		dataTableColumn.setText("套餐编号");
		dataTableColumn.setWidth(65);

		DataTableColumn dataTableColumn_1 = new DataTableColumn(dataTable,
				SWT.NONE, "tcmc");
		dataTableColumn_1.setText("名称");
		dataTableColumn_1.setWidth(100);

		DataTableColumn dataTableColumn_2 = new DataTableColumn(dataTable,
				SWT.RIGHT, "sj");
		dataTableColumn_2.setText("售价");
		dataTableColumn_2.setWidth(50);

		DataTableColumn dataTableColumn_3 = new DataTableColumn(dataTable,
				SWT.CENTER, "fwxm");
		dataTableColumn_3.setText("包含服务项目");
		dataTableColumn_3.setWidth(220);

		Composite composite = new Composite(shell, SWT.NONE);
		GridLayout gl_composite = new GridLayout(5, false);
		gl_composite.horizontalSpacing = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblNewLabel = new Label(composite, SWT.NONE);
		GridData gd_lblNewLabel = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel.horizontalIndent = 10;
		lblNewLabel.setLayoutData(gd_lblNewLabel);
		lblNewLabel.setText("查询条件：");

		text = new Text(composite, SWT.BORDER);
		text.setMessage("在些输入查询条件");
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1);
		gd_text.widthHint = 120;
		text.setLayoutData(gd_text);

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setParams();
				dataset.update();
				if (dataset.getCount() > 0) {
					dataset.setSelected(0);
				}
			}
		});
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.horizontalIndent = 6;
		gd_btnNewButton.widthHint = 70;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("查询");

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doOk();
			}
		});
		GridData gd_btnNewButton_1 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_1.horizontalIndent = 6;
		gd_btnNewButton_1.widthHint = 70;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setText("选择");

		Button btnNewButton_2 = new Button(composite, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		GridData gd_btnNewButton_2 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_2.horizontalIndent = 6;
		gd_btnNewButton_2.widthHint = 70;
		btnNewButton_2.setLayoutData(gd_btnNewButton_2);
		btnNewButton_2.setText("关闭");
		setParams();
		createGUIAfter();

	}

	protected void doOk() {
		setParams();
		tcfwxmSet.update();
		result = tcfwxmSet;
		shell.close();
	}

	protected void setParams() {
		dataset.setParam(0, text.getText());
		dataset.setParam(1, text.getText());
		dataset.setParam(2, text.getText());
		dataset.setParam(3, text.getText());
		tcfwxmSet.setParam(0,dataset.getValue("tcbh"));
	}

	public static void main(String[] args) {
		XztcDialog dlg = new XztcDialog(new Shell(), SWT.NONE);
		dlg.open();
	}

	protected void createGUIAfter() {
		dataset.update();
		if (dataset.getCount() > 0) {
			dataset.setSelected(0);
		}
	}
}
